package com.rc.saas.tenant.service.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.utils.DateHelper;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.mapper.order.OrderDetailMapper;
import com.rc.saas.tenant.model.elec.ElecStationPrice;
import com.rc.saas.tenant.model.order.OrderDetail;
import com.rc.saas.tenant.model.order.OrderDetailExample;
import com.rc.saas.tenant.search.order.OrderDetailSearch;
import com.rc.saas.tenant.service.tenant.TenantRoleService;
import com.rc.saas.tenant.vo.order.OrderDetailPrintVo;
import com.rc.saas.tenant.vo.order.OrderDetailResult;
import com.rc.saas.tenant.vo.statistics.StatisticsQueryVo;
import com.rc.saas.tenant.vo.tenant.TenantUserRoleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by sven on 2018-04-17
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;


    @Autowired
    private TenantRoleService tenantRoleService;

    @Override
    public OrderDetailMapper getMapper() {
        return orderDetailMapper;
    }

    @Override
    public OrderDetail selectOrderDetailByMigrateId(String tenantCode, Long migrateId) {
        OrderDetailExample example = new OrderDetailExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andMigrateIdEqualTo(migrateId);
        List<OrderDetail> list = orderDetailMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<OrderDetailPrintVo> exportOrderDetail(OrderDetailSearch orderDetailSearch) {
        List<OrderDetailPrintVo> orderDetailResults = orderDetailMapper.exportOrderDetail(orderDetailSearch);
        return orderDetailResults;
    }

    @Override
    public PageInfo<OrderDetailResult> searchByCondition(OrderDetailSearch orderDetailSearch) {

        TenantUserRoleVo userRoleVo = tenantRoleService.getLonginRole(orderDetailSearch.getLoginName());
        if (userRoleVo!=null){
            orderDetailSearch.setRoleType(userRoleVo.getRoleType());
            orderDetailSearch.setDataAuth(userRoleVo.getDataAuth());
            switch (userRoleVo.getRoleType()){
                case "1"://自定义用户
                    orderDetailSearch.setOperatorIds(
                            StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                                    Arrays.asList(userRoleVo.getOperatorIds().split(","))
                                    :new ArrayList<String>());
                    orderDetailSearch.setEnterpriseIds(
                            StringUtils.isNotBlank(userRoleVo.getEnterpriseIds())?
                                    Arrays.asList(userRoleVo.getEnterpriseIds().split(","))
                                    :new ArrayList<String>());
                    break;
                case "2"://加盟商用户
                    orderDetailSearch.setOperatorIds(StringUtils.isNotBlank(userRoleVo.getOperatorIds())?
                            Arrays.asList(userRoleVo.getOperatorIds().split(","))
                            :new ArrayList<String>());
                    break;
                case "3"://企业用户
                    orderDetailSearch.setEnterpriseIds(StringUtils.isNotBlank(userRoleVo.getEnterpriseIds())?
                            Arrays.asList(userRoleVo.getEnterpriseIds().split(","))
                            :new ArrayList<String>());
                    break;
            }
        }
        PageHelper.startPage(orderDetailSearch.getOffset(), orderDetailSearch.getLimit());
        List<OrderDetailResult> orderDetailResultVoList = orderDetailMapper.searchByCondition(orderDetailSearch);
        return new PageInfo<OrderDetailResult>(orderDetailResultVoList);
    }

    @Override
    public List<OrderDetailPrintVo> exportOperatorPowerTimePeriod(List<ElecStationPrice> list, StatisticsQueryVo statisticsQueryVO) {
        String begin = statisticsQueryVO.getStartTime();
        String end = statisticsQueryVO.getEndTime();

        if (StringUtil.isBlank(statisticsQueryVO.getEndTime())) {
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            end = format.format(date);
        } else {
            end += " 23:59:59";
        }

        List<OrderDetailPrintVo> operatorPowerTimePeriodVo = new ArrayList<OrderDetailPrintVo>();

        for (int i = 0; i < list.size(); i++) {
            ElecStationPrice price = list.get(i);

            String bsBegin = DateHelper.dataToString(price.getBeginTime(), DateHelper.DATE_FORMAT_HHMMSS);
            String bsEnd = DateHelper.dataToString(price.getEndTime(), DateHelper.DATE_FORMAT_HHMMSS);

            if (bsEnd.equals("00:00:00")) {
                bsEnd = "23:59:59";
            }

            String bsBeginHHmm = DateHelper.dataToString(price.getBeginTime(), DateHelper.DATE_FORMAT_HHMM);
            String bsEndHHmm = DateHelper.dataToString(price.getEndTime(), DateHelper.DATE_FORMAT_HHMM);

            String priceCode = price.getPriceCode();

            if ("1".equals(priceCode)) {
                priceCode = "尖峰";
            } else if ("2".equals(priceCode)) {
                priceCode = "高峰";
            } else if ("3".equals(priceCode)) {
                priceCode = "平段";
            } else if ("4".equals(priceCode)) {
                priceCode = "低谷";
            }

            bsBeginHHmm = priceCode + " " + bsBeginHHmm + "~" + bsEndHHmm;

            List<OrderDetailPrintVo> voList = orderDetailMapper.selectOperatorPowerTimePeriod(statisticsQueryVO.getElecStationId(), begin, end, bsBegin, bsEnd, bsBeginHHmm);
            operatorPowerTimePeriodVo.addAll(voList);
        }
        return operatorPowerTimePeriodVo;
    }

}
